import { myBox2D, Rect, Circular, Polygon, Complex, RevoluteJoint } from "../chapter4/mybox2d";
import * as Box2D from "box2d.ts";
import { Game, loader } from "../chapter2/game";
export class GameBody {
  type: string | null = null;
  name: string | null = null;
  x: number | null = null;
  y: number | null = null;
  angle: number | null = null;
  shape: string | null = null;
  fullHealth: number | null = null;
  radius: number | null = null;
  density: number | null = null;
  friction: number | null = null;
  restitution: number | null = null;
  width: number;
  height: number;
  isStatic: boolean = false;
  calories: number = 0;

  box2d: myBox2D | null = null;
  game: Game;

  constructor(option: { type: string; name: string; x: number; y: number; angle: number; shape: string | null; fullHealth: number; radius: number; density: number; friction: number; restitution: number; width: number; height: number; isStatic: boolean; calories?: number }, game: Game) {
    this.type = option.type;
    this.name = option.name;
    this.x = option.x;
    this.y = option.y;
    this.angle = option.angle;
    this.shape = option.shape;
    this.fullHealth = option.fullHealth;
    this.radius = option.radius;
    this.density = option.density;
    this.friction = option.friction;
    this.restitution = option.restitution;
    this.width = option.width;
    this.height = option.height;
    this.isStatic = option.isStatic;
    if (option.calories) {
      this.calories = option.calories;
    }
    this.game = game;
    this.box2d = this.game.box2d;
    this.create();
  }

  create() {
    let bodyType: number;
    if (this.isStatic) {
      bodyType = Box2D.b2BodyType.b2_staticBody;
    } else {
      bodyType = Box2D.b2BodyType.b2_dynamicBody;
    }
    switch (this.type) {
      case "block": //简单矩形
        let image = new Image();
        image.src = "/img/images/entities/" + this.name! + ".png";
        image.onload = () => {
          new Rect({
            world: this.box2d!.world!,
            position: { x: this.x!, y: this.y! },
            density: this.density!,
            friction: this.friction!,
            restitution: this.restitution!,
            size: { x: this.width, y: this.height },
            type: bodyType,
            userdata: { name: "polygon", sprite: image },
          });
        };
        break;

      default:
        break;
    }
  }
}

//游戏物体数据
export let levelData_1: {
  type: string;
  name: string;
  x: number;
  y: number;
  angle: number;
  shape: string | null;
  fullHealth: number;
  radius: number;
  density: number;
  friction: number;
  restitution: number;
  width: number;
  height: number;
  isStatic: boolean;
  calories?: number;
}[];
levelData_1 = [
  {
    type: "ground",
    name: "dirt",
    x: 500,
    y: 440,
    angle: 0,
    shape: null,
    fullHealth: 0,
    radius: 0,
    density: 3.0,
    friction: 1.5,
    restitution: 0.2,
    width: 1000,
    height: 20,
    isStatic: true,
  },
  {
    type: "ground",
    name: "wood",
    x: 190,
    y: 390,
    angle: 0,
    shape: null,
    fullHealth: 500,
    radius: 0,
    density: 0.7,
    friction: 0.4,
    restitution: 0.4,
    width: 30,
    height: 80,
    isStatic: true,
  },
  {
    type: "block",
    name: "wood",
    x: 190,
    y: 390,
    angle: 0,
    shape: null,
    fullHealth: 500,
    radius: 0,
    density: 0.7,
    friction: 0.4,
    restitution: 0.4,
    width: 30,
    height: 80,
    isStatic: true,
  },
  {
    type: "block",
    name: "glass",
    x: 500,
    y: 280,
    angle: 90,
    shape: null,
    fullHealth: 100,
    radius: 0,
    density: 2.4,
    friction: 0.4,
    restitution: 0.15,
    width: 100,
    height: 25,
    isStatic: false,
  },
  {
    type: "villain",
    name: "burger",
    x: 500,
    y: 205,
    angle: 90,
    shape: "circle",
    fullHealth: 40,
    radius: 25,
    density: 1,
    friction: 0.5,
    restitution: 0.4,
    width: 100,
    height: 25,
    isStatic: false,
    // calories: 590,
  },
  {
    type: "block",
    name: "wood",
    x: 800,
    y: 380,
    angle: 90,
    shape: "circle",
    fullHealth: 500,
    radius: 25,
    density: 0.7,
    friction: 0.4,
    restitution: 0.4,
    width: 100,
    height: 25,
    isStatic: false,
  },
  {
    type: "block",
    name: "glass",
    x: 800,
    y: 280,
    angle: 90,
    shape: "circle",
    fullHealth: 100,
    radius: 25,

    density: 2.4,
    friction: 0.4,
    restitution: 0.15,
    width: 100,
    height: 25,
    isStatic: false,
  },
  {
    type: "villain",
    name: "fries",
    x: 800,
    y: 205,
    angle: 90,
    shape: "rectangle",
    fullHealth: 50,
    radius: 25,

    density: 1,
    friction: 0.5,
    restitution: 0.6,
    width: 40,
    height: 50,
    isStatic: false,
    calories: 420,
  },
  {
    type: "hero",
    name: "orange",
    x: 80,
    y: 405,
    angle: 90,
    shape: "circle",
    fullHealth: 50,
    radius: 25,

    density: 1.5,
    friction: 0.5,
    restitution: 0.4,
    width: 40,
    height: 50,
    isStatic: false,
    calories: 420,
  },
  {
    type: "hero",
    name: "apple",
    x: 140,
    y: 405,
    angle: 90,
    shape: "circle",
    fullHealth: 50,
    radius: 25,
    density: 1.5,
    friction: 0.5,
    restitution: 0.4,
    width: 40,
    height: 50,
    isStatic: false,
    calories: 420,
  },
];
